﻿#include "EditorScenePageView.h"
#include "EditorScenePageViewModel.h"
#include "Widgets/ViewportWidget.h"
#include "Widgets/TextWidget.h"

#include "Widgets/Keyboard/KeyboardWidget.h"

#include "Widgets/Positioners/HorizontalPositionerWidget.h"
#include "Widgets/EditorImageButton.h"

#include "Framework/Editor.h"
#include "Theme/EditorTheme.h"


void REditorScenePageView::BindViewModel(TSharedObjectPtr<REditorPageViewModel> InViewModel)
{
    //Super::BindViewModel(InViewModel);
    ViewModel = InViewModel;
}

void REditorScenePageView::CreateWidget(TSharedObjectPtr<RWidget> InParent)
{
    //create a  viewport widget
    auto PreviewViewport=NewWidget<RViewportWidget>(InParent);
    PreviewViewport->SetScene(ViewModel->GetScene());
    PreviewViewport->Anchor.Fill(InParent);
    PreviewViewport->bForceUseDefaultParameters=true;

    PreviewViewport->ViewportPosition=[this]()->SVec3
    {
        return ViewModel->ViewPosition;
    };

    PreviewViewport->ViewportRotation=[this]()->SQuat
    {
        return ViewModel->ViewRotation;
    };


    PreviewViewport->PaintMode=[this]()->EViewportPaintMode
    {
        if(ViewModel->ShowAxis())
        {
            return (EViewportPaintMode)((int)EViewportPaintMode::Scene| (int)EViewportPaintMode::Foreground);
        }
        else
        {
            return EViewportPaintMode::Scene;
        }
    };

    // PreviewViewport->AxisVisible=[this]()->bool
    // {
    //     return ViewModel->ShowAxis();
    // };
    PreviewViewport->AxisOrigin=[this]()->SVec3
    {
        return ViewModel->GetAxisPosition();
    };
    PreviewViewport->AxisRotation=[this]()->SQuat
    {
        return ViewModel->GetAxisRotation();
    };
    PreviewViewport->AxisScale=[this]()->float
    {
        return ViewModel->GetAxisScale();
    };

    RViewportWidgetWeakPtr PreviewViewportWeak=PreviewViewport;
    PreviewViewport->PixelObjectMap=[this,PreviewViewportWeak]()->TSharedObjectPtr<RPixelObjectMap>
    {
        return ViewModel->GetPixelObjectMap(PreviewViewportWeak);
    };
    


    //create a widget to let user to rotate view
    auto Mouse =NewWidget<RMouseWidget>(InParent);
    Mouse->Anchor.Fill(InParent);

    Mouse->Drag->bIsEnabled=true;

    Mouse->Drag->OnDragStarted.Bind([this,PreviewViewportWeak]()
    {
        ViewModel->BeginMouseDrag(PreviewViewportWeak);
    });

    Mouse->Drag->OnDragMoved.Bind([this,PreviewViewportWeak](const SVec2& InMouseDelta)
    {
        ViewModel->OnMouseDrag(PreviewViewportWeak,InMouseDelta);
    });

    Mouse->Drag->OnDragEnded.Bind([this]()
    {
        ViewModel->EndMouseDrag();
    });

    auto Keyboard=NewWidget<RKeyboardWidget>(InParent);
    Mouse->AutoFocusTarget=Keyboard;
    Keyboard->Anchor.Fill(Mouse);

    Keyboard->OnKeyboardInput.Bind([this](const SKeyboardEvent& InEvent)->bool
    {
        if(InEvent.KeyState!=EKeyState::Pressed)
        {
            return false;
        }


        const float MoveSpeed=0.1f;
        switch(InEvent.Key)
        {
            case EKey::W:
            {
                ViewModel->ViewMoveForward();
                return true;
            }
            case EKey::S:
            {
                ViewModel->ViewMoveBackward();
                return true;
            }
            case EKey::A:
            {
                ViewModel->ViewMoveLeft();
                return true;
            }
            case EKey::D:
            {
                ViewModel->ViewMoveRight();
                return true;
            }
            case EKey::Q:
            {
                ViewModel->ViewMoveUp();
                return true;
            }
            case EKey::E:
            {
                ViewModel->ViewMoveDown();
                return true;
            }

        }


        return false;
    });

}

void REditorScenePageView::FillOperations(TSharedObjectPtr<RWidget> InOperationParent)
{
    auto OperationParent=NewWidget<RHorizontalPositionerWidget>(InOperationParent);
    OperationParent->Anchor.VerticalFill(InOperationParent);
    OperationParent->Spacing= REditor::Get()->CurrentTheme->PageAreaButtonInterval;

    WHE:: SImageButtonCreateInfo CreateInfo;
    CreateInfo.ParentWidget=OperationParent;
    CreateInfo.Size=REditor::Get()->CurrentTheme->PageAreaButtonSize;
    CreateInfo.IconSize=REditor::Get()->CurrentTheme->PageAreaButtonIconSize;
    CreateInfo.OnClick=[this](){
        ViewModel->OnPlayButtonClicked();
    };
    CreateInfo.Icon=[this]()->TSharedObjectPtr<RTexture2D>
    {
        return ViewModel->GetPlayButtonIcon();
    };
    CreateInfo.ToolTip=[this]()->CString
    {
        return ViewModel->GetPlayButtonTooltip();
    };

    auto PlayButton = WHE::CreateImageButton(CreateInfo);

}
